package com.key.controller.admin.assetManagement;

import com.key.common.R;
import com.key.common.dto.ClearFormFindDTO;
import com.key.common.vo.ClearFormVO;
import com.key.entity.Assets;
import com.key.entity.ClearForm;
import com.key.entity.RepairForm;
import com.key.service.ClearFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author wei
 * @create 2023-02-17 15:56
 */
@Controller
@Api(value = "清理报废API",tags = "清理报废")
@RequestMapping("/clearForm")
public class ClearFormController {

    @Autowired
    ClearFormService clearFormService;

    /**
     * 添加报废清单
     * @author wei
     * @param clearForm
     * @return
     */
    @RequestMapping(value = "addClearForm", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "添加报废",notes = "获取前端页面的信息以完成新建",httpMethod = "post", response = R.class)
    public R addClearForm(@RequestBody @ApiParam(name = "ClearForm",value = "新建清理报废清单需要填入的属性",required = true) ClearForm clearForm){
        R r;

        boolean result = clearFormService.addClearForm(clearForm);

        if(result){
            r = new R(200,"添加成功");
        }else {
            r = new R(500,"添加失败");
        }

        return r;
    }

    /**
     * 删除报废清单
     * @author wei
     * @param cfNo
     * @return
     */
    @RequestMapping(value = "/removeClearForm", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除报废单",notes = "获取维修单号以删除", httpMethod = "post", response = R.class)
    @ApiImplicitParam(name = "cfNo",value = "维修单号",required = true,dataType = "String",paramType = "query")
    public R removeClearForm(@RequestParam(name = "cfNo") String cfNo){
        R r;

        boolean result = clearFormService.removeClearForm(cfNo);

        if(result){
            r = new R(200,"删除成功");
        }else {
            r = new R(500,"删除失败");
        }

        return r;
    }

    /**
     * 修改报废清单
     * @author wei
     * @param clearForm
     * @return
     */
    @RequestMapping(value = "/editClearForm", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改报废单",notes = "获取前端页面的信息以完成修改",httpMethod = "post", response = R.class)
    public R editClearForm(@RequestBody @ApiParam(name = "RepairForm",value = "新建维修清单需要填入的属性",required = true) ClearForm clearForm){
        R r;

        boolean result = clearFormService.editClearForm(clearForm);

        if(result){
            r = new R(200,"修改成功");
        }else {
            r = new R(500,"修改失败");
        }

        return r;
    }

    /**
     * 查找报废清单(含高级查询)
     * @author wei
     * @param clearFormFindDTO
     * @return
     */
    @RequestMapping(value = "/findClearForm", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询报废单",notes = "查询维修单，可高级查询",httpMethod = "post", response = R.class)
    public R findClearForm(@RequestBody @ApiParam(name = "ClearFormFindDTO",value = "新建维修清单需要填入的属性",required = true) ClearFormFindDTO clearFormFindDTO){
        R r = new R();

        try{
            Map<String, Object> map = clearFormService.findClearForm(clearFormFindDTO);
            List<ClearForm> clearFormList = (List<ClearForm>) map.get("clearFormList");
            Integer count = (Integer) map.get("count");

            r.setCode(200);
            r.setCount(count);
            r.setMsg("查询成功");
            r.setData(clearFormList);
        }catch (Exception e){
            r.setCode(500);
            r.setMsg("后台代码异常");
        }

        return r;
    }

    /**
     * 点击清理报废单号，显示详细信息
     * @param cfNo
     * @return
     */
    @RequestMapping(value = "/displayClearForm", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "点击清理报废单号",notes = "点击清理报废单号，显示详细信息",httpMethod = "get", response = R.class)
    @ApiImplicitParam(name = "cfNo",value = "维修单号",required = true,dataType = "String",paramType = "query")
    public R displayClearForm(@RequestParam(name = "cfNo") String cfNo){
        R r = new R();

        try{
            ClearFormVO clearFormVO = clearFormService.findClearFormInfoByCfNo(cfNo);

            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(clearFormVO);
        }catch (Exception e){
            r.setCode(500);
            r.setMsg("后台代码异常");
        }

        return r;
    }

    /**
     * 导出excel表格
     * @param request
     * @param response
     * @throws Exception
     */
    @RequestMapping(value = "/exportExcel", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出excel表格",notes = "导出excel表格",httpMethod = "get", response = R.class)
    public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
        HSSFWorkbook workBook = clearFormService.getWorkBook();
        String userAgent = request.getHeader("User-Agent");

        //excel文件名
        String fileName = "清理报废表"+System.currentTimeMillis()+".xls";
        // 针对IE或者以IE为内核的浏览器：
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            fileName = java.net.URLEncoder.encode(fileName, "UTF-8");
        } else {
            // 非IE浏览器的处理：
            fileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
        }
        //响应到客户端
        try {
            OutputStream os = response.getOutputStream();
            response.setContentType("application/x-xls;charset=utf-8");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            workBook.write(os);
            os.flush();
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
